import request from "@/utils/request";

const state = {
  companyEmployeeList: [],
  companyAdminList: [],
  companyRoleList: [],
  companyPositionBaseList: [],
  companyPositionList: [],
  companyDepartmentBaseList: [],
  companyDepartmentList: [],
  relationDataLoaded: false,
  companyDefSource: [],
  companyPosSource: []
};

const mutations = {
  updateEmployees: (state, payload) => {
    state.companyEmployeeList = payload;
  },
  updateAdmins: (state, payload) => {
    state.companyAdminList = payload;
  },
  updateRoles: (state, payload) => {
    state.companyRoleList = payload;
  },
  updatePositions: (state, payload) => {
    state.companyPositionBaseList = payload;
    state.companyPositionList = buildPosTree(payload, state.companyId);

    if (payload.length > 0) {
      const posTree = buildIterTree(payload, "0");
      // 岗位第一级不能选择
      posTree[0].children = posTree[0].children.map((z) => {
        z.disabled = true;
        return z;
      });
      state.companyPosSource = posTree[0].children || [];
    } else {
      state.companyPosSource = [];
    }
  },
  updateDepartments: (state, payload) => {
    state.companyDepartmentBaseList = payload;
    state.companyDepartmentList = buildPosTree(payload, state.companyId);
    state.relationDataLoaded = true;

    if (payload.length > 0) {
      const topItem = payload.filter((item) => item.isTop)[0];
      const depTree = buildIterTree(payload, topItem.parentId);
      state.companyDefSource = depTree[0].children || [];
    } else {
      state.companyDefSource = [];
    }
  }
};

const actions = {
  loadEmployeesAct: ({ commit }, companyId) => {
    request({
      url: `/hr/api/v1/empInfos/listAll?companyId=${companyId}`
    })
      .then((res) => {
        commit("updateEmployees", res.data);
      })
      .catch((err) => {
        console.log("debug log --> ", err);
      });
  },
  loadAdminsAct: ({ commit }, companyId) => {
    request({
      url: `/hr/api/v1/users/listUserHasRoleInCompany/${companyId}`
    })
      .then((res) => {
        commit("updateAdmins", res.data);
      })
      .catch((err) => {
        console.log("debug log --> ", err);
      });
  },
  loadRolesAct: ({ commit }) => {
    request({
      url: `/hr/api/v1/permissions/roleList?subjectType=company`
    })
      .then((res) => {
        commit("updateRoles", res.data);
      })
      .catch((err) => {
        console.log("debug log --> ", err);
      });
  },
  loadPositionsAct: ({ commit }, companyId) => {
    request({
      url: `/hr/api/v1/positionInfos/listAll?companyId=${companyId}`
    })
      .then((res) => {
        commit("updatePositions", res.data);
      })
      .catch((err) => {
        console.log("debug log --> ", err);
      });
  },
  loadDepartmentsAct: ({ commit }, companyId) => {
    request({
      url: `/hr/api/v1/depInfos/listAll?companyId=${companyId}`
    })
      .then((res) => {
        commit("updateDepartments", res.data);
      })
      .catch((err) => {
        console.log("debug log --> ", err);
      });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};

const buildPosTree = (data, parentId) => {
  const result = [];
  let temp = null;
  for (let i = 0; i < data.length; i++) {
    if (data[i].parentId === parentId) {
      const obj = {
        value: data[i]["id"],
        key: data[i]["id"],
        title: data[i]["name"],
        item: data[i]
      };
      temp = buildPosTree(data, data[i].id);
      if (temp.length > 0) {
        obj.children = temp;
      } else {
        obj.children = [];
      }
      result.push(obj);
    }
  }
  return result;
};

function buildIterTree(data, parentId) {
  const result = [];
  let temp = null;
  for (let i = 0; i < data.length; i++) {
    if (data[i].parentId === parentId) {
      const obj = {
        value: data[i]["id"],
        key: data[i]["id"],
        title: data[i]["name"],
        item: data[i]
      };
      temp = buildIterTree(data, data[i].id);
      if (temp.length > 0) {
        obj.children = temp;
      } else {
        obj.children = [];
      }
      result.push(obj);
    }
  }
  return result;
}
